<template>
  <div v-if="showGuide" class="user-guide-overlay">
    <!-- 引导遮罩层 -->
    <div class="guide-backdrop" @click="skipGuide"></div>
    
    <!-- 引导内容卡片 -->
    <div class="guide-card" :class="currentStep.position">
      <!-- 引导标题 -->
      <div class="guide-header">
        <h3 class="guide-title">{{ currentStep.title }}</h3>
        <button @click="skipGuide" class="guide-close">
          <font-awesome-icon :icon="['fas', 'times']" />
        </button>
      </div>
      
      <!-- 引导内容 -->
      <div class="guide-content">
        <div class="guide-icon">
          <font-awesome-icon :icon="currentStep.icon" class="text-4xl text-blue-500" />
        </div>
        <p class="guide-description">{{ currentStep.description }}</p>
        
        <!-- 特殊步骤的额外内容 -->
        <div v-if="currentStep.id === 'welcome'" class="welcome-features">
          <div class="feature-list">
            <div class="feature-item">
              <font-awesome-icon :icon="['fas', 'brain']" class="feature-icon" />
              <span>智能心理分析</span>
            </div>
            <div class="feature-item">
              <font-awesome-icon :icon="['fas', 'comments']" class="feature-icon" />
              <span>24/7在线陪伴</span>
            </div>
            <div class="feature-item">
              <font-awesome-icon :icon="['fas', 'shield-alt']" class="feature-icon" />
              <span>隐私安全保护</span>
            </div>
          </div>
        </div>
        
        <!-- 示例对话 -->
        <div v-if="currentStep.id === 'chat-demo'" class="chat-demo">
          <div class="demo-message user-message">
            <div class="message-avatar">👤</div>
            <div class="message-content">我最近感觉很焦虑，不知道该怎么办...</div>
          </div>
          <div class="demo-message bot-message">
            <div class="message-avatar">🤖</div>
            <div class="message-content">我理解你的感受。焦虑是很常见的情绪反应。让我们一起探讨一下...</div>
          </div>
        </div>
      </div>
      
      <!-- 引导操作按钮 -->
      <div class="guide-actions">
        <button 
          v-if="currentStepIndex > 0" 
          @click="previousStep" 
          class="guide-btn guide-btn-secondary"
        >
          上一步
        </button>
        
        <div class="guide-progress">
          <span class="progress-text">{{ currentStepIndex + 1 }} / {{ guideSteps.length }}</span>
          <div class="progress-bar">
            <div 
              class="progress-fill" 
              :style="{ width: `${((currentStepIndex + 1) / guideSteps.length) * 100}%` }"
            ></div>
          </div>
        </div>
        
        <button 
          @click="nextStep" 
          class="guide-btn guide-btn-primary"
        >
          {{ isLastStep ? '开始使用' : '下一步' }}
        </button>
      </div>
      
      <!-- 跳过引导 -->
      <div class="guide-skip">
        <button @click="skipGuide" class="skip-link">跳过引导</button>
      </div>
    </div>
    
    <!-- 高亮指示器 -->
    <div 
      v-if="currentStep.target" 
      class="guide-highlight"
      :style="highlightStyle"
    ></div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, nextTick } from 'vue'
import { useUserStore } from '@/stores/user'

// Props
const props = defineProps({
  autoStart: {
    type: Boolean,
    default: true
  }
})

// Emits
const emit = defineEmits(['guide-completed', 'guide-skipped'])

// Store
const userStore = useUserStore()

// 响应式数据
const showGuide = ref(false)
const currentStepIndex = ref(0)
const highlightStyle = ref({})

// 引导步骤配置
const guideSteps = ref([
  {
    id: 'welcome',
    title: '欢迎使用心理健康助手',
    description: '我是您的专业心理健康伙伴，提供24小时贴心陪伴和专业建议。让我们开始这段心理健康之旅吧！',
    icon: ['fas', 'heart'],
    position: 'center',
    target: null
  },
  {
    id: 'chat-area',
    title: '对话区域',
    description: '这里是我们的主要对话区域。您可以在这里与我分享您的想法、感受和困扰。',
    icon: ['fas', 'comments'],
    position: 'bottom-left',
    target: '.chat-messages'
  },
  {
    id: 'input-area',
    title: '消息输入',
    description: '在这里输入您想要分享的内容。支持文字输入，我会认真倾听并给出专业建议。',
    icon: ['fas', 'keyboard'],
    position: 'top-center',
    target: '.message-input'
  },
  {
    id: 'sidebar',
    title: '对话历史',
    description: '左侧边栏显示您的对话历史。您可以随时回顾之前的对话内容。',
    icon: ['fas', 'history'],
    position: 'right',
    target: '.sidebar'
  },
  {
    id: 'features',
    title: '智能功能',
    description: '我具备情绪分析、心理评估、专业建议等功能，会根据您的需求提供个性化帮助。',
    icon: ['fas', 'brain'],
    position: 'center',
    target: null
  },
  {
    id: 'chat-demo',
    title: '开始对话',
    description: '现在您可以开始与我对话了！试着分享您今天的感受，或者询问任何心理健康相关的问题。',
    icon: ['fas', 'play-circle'],
    position: 'center',
    target: null
  }
])

// 计算属性
const currentStep = computed(() => guideSteps.value[currentStepIndex.value])
const isLastStep = computed(() => currentStepIndex.value === guideSteps.value.length - 1)

// 方法
const startGuide = () => {
  showGuide.value = true
  currentStepIndex.value = 0
  updateHighlight()
}

const nextStep = () => {
  if (isLastStep.value) {
    completeGuide()
  } else {
    currentStepIndex.value++
    updateHighlight()
  }
}

const previousStep = () => {
  if (currentStepIndex.value > 0) {
    currentStepIndex.value--
    updateHighlight()
  }
}

const skipGuide = () => {
  showGuide.value = false
  markGuideAsCompleted()
  emit('guide-skipped')
}

const completeGuide = () => {
  showGuide.value = false
  markGuideAsCompleted()
  emit('guide-completed')
}

const markGuideAsCompleted = () => {
  // 标记引导已完成
  localStorage.setItem('user-guide-completed', 'true')
  if (userStore.userInfo) {
    localStorage.setItem(`user-guide-completed-${userStore.userInfo.id}`, 'true')
  }
}

const isGuideCompleted = () => {
  // 检查引导是否已完成
  const globalCompleted = localStorage.getItem('user-guide-completed') === 'true'
  if (userStore.userInfo) {
    const userCompleted = localStorage.getItem(`user-guide-completed-${userStore.userInfo.id}`) === 'true'
    return globalCompleted || userCompleted
  }
  return globalCompleted
}

const updateHighlight = async () => {
  await nextTick()
  
  if (!currentStep.value.target) {
    highlightStyle.value = {}
    return
  }
  
  const targetElement = document.querySelector(currentStep.value.target)
  if (targetElement) {
    const rect = targetElement.getBoundingClientRect()
    highlightStyle.value = {
      position: 'fixed',
      top: `${rect.top - 8}px`,
      left: `${rect.left - 8}px`,
      width: `${rect.width + 16}px`,
      height: `${rect.height + 16}px`,
      borderRadius: '8px',
      border: '3px solid #3b82f6',
      boxShadow: '0 0 0 9999px rgba(0, 0, 0, 0.5)',
      pointerEvents: 'none',
      zIndex: 9998
    }
  }
}

// 生命周期
onMounted(() => {
  if (props.autoStart && !isGuideCompleted()) {
    // 延迟启动，确保页面加载完成
    setTimeout(() => {
      startGuide()
    }, 1000)
  }
})

// 暴露方法给父组件
defineExpose({
  startGuide,
  skipGuide,
  isGuideCompleted
})
</script>

<style scoped>
.user-guide-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  pointer-events: none;
}

.guide-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  pointer-events: all;
}

.guide-card {
  position: fixed;
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  max-width: 400px;
  width: 90vw;
  pointer-events: all;
  z-index: 10000;
  animation: guideSlideIn 0.3s ease-out;
}

/* 位置样式 */
.guide-card.center {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.guide-card.bottom-left {
  bottom: 20px;
  left: 20px;
}

.guide-card.top-center {
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
}

.guide-card.right {
  top: 50%;
  right: 20px;
  transform: translateY(-50%);
}

.guide-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 20px 0;
}

.guide-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

.guide-close {
  background: none;
  border: none;
  color: #6b7280;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s;
}

.guide-close:hover {
  background: #f3f4f6;
  color: #374151;
}

.guide-content {
  padding: 20px;
  text-align: center;
}

.guide-icon {
  margin-bottom: 16px;
}

.guide-description {
  color: #4b5563;
  line-height: 1.6;
  margin-bottom: 20px;
}

.welcome-features {
  margin-top: 20px;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  background: #f8fafc;
  border-radius: 8px;
}

.feature-icon {
  color: #3b82f6;
  width: 20px;
}

.chat-demo {
  margin-top: 20px;
  text-align: left;
}

.demo-message {
  display: flex;
  gap: 12px;
  margin-bottom: 12px;
  padding: 12px;
  border-radius: 8px;
}

.user-message {
  background: #eff6ff;
}

.bot-message {
  background: #f0fdf4;
}

.message-avatar {
  font-size: 1.5rem;
  flex-shrink: 0;
}

.message-content {
  font-size: 0.875rem;
  color: #374151;
  line-height: 1.4;
}

.guide-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px 20px;
  gap: 16px;
}

.guide-btn {
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
}

.guide-btn-primary {
  background: #3b82f6;
  color: white;
}

.guide-btn-primary:hover {
  background: #2563eb;
}

.guide-btn-secondary {
  background: #f3f4f6;
  color: #374151;
}

.guide-btn-secondary:hover {
  background: #e5e7eb;
}

.guide-progress {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex: 1;
}

.progress-text {
  font-size: 0.75rem;
  color: #6b7280;
}

.progress-bar {
  width: 100%;
  height: 4px;
  background: #e5e7eb;
  border-radius: 2px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: #3b82f6;
  transition: width 0.3s ease;
}

.guide-skip {
  text-align: center;
  padding: 0 20px 16px;
}

.skip-link {
  background: none;
  border: none;
  color: #6b7280;
  font-size: 0.875rem;
  cursor: pointer;
  text-decoration: underline;
}

.skip-link:hover {
  color: #374151;
}

.guide-highlight {
  transition: all 0.3s ease;
}

@keyframes guideSlideIn {
  from {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 640px) {
  .guide-card {
    max-width: none;
    width: calc(100vw - 32px);
    margin: 16px;
  }
  
  .guide-card.bottom-left,
  .guide-card.top-center,
  .guide-card.right {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}
</style>
